package com.world.cat.controller.back;

import com.world.cat.enums.CategoryType;
import com.world.cat.service.generate.FieldService;
import com.world.common.pojo.WorldPage;
import com.world.common.pojo.Result;
import com.world.common.pojo.tree.ZTreeNode;
import com.world.common.util.SysUtil;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.world.common.util.NumberUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.world.cat.service.back.*;
import com.world.cat.model.back.Category;

@Controller
@RequestMapping(value = "/back/category")
public class CategoryController {
    @Resource
    private CategoryService service;

    @RequestMapping(value = "/home")
    public String home(Model model) {
        return "/back/category/home";
    }
    
    @RequestMapping(value = "/save")
    @ResponseBody
    public Object save(Category category) {
        if (category.getSeq() == null) {
            return new Result(false, "必填参数不能为空");
        }
        if (!SysUtil.isAllNotEmpty(category.getName(), category.getParentId())) {
            return new Result(false, "必填参数不能为空");
        }
        Category categoryDb = service.saveCategory(category);
        ZTreeNode treeNode = new ZTreeNode();
        treeNode.setChecked(false);
        treeNode.setName(categoryDb.getName());
        treeNode.setId(categoryDb.getId());
        treeNode.setpId(categoryDb.getParentId());
        return new Result(true, "", treeNode);
    }

    @RequestMapping(value = "/categoryTree", name = "分类树")
    @ResponseBody
    public Object categoryTree(CategoryType categoryType) {
        ZTreeNode treeNode = service.getCategoryTree(categoryType);
        List<ZTreeNode> nodes = new ArrayList<>();
        nodes.add(treeNode);
        return nodes;
    }

    @RequestMapping(value = "/del")
    @ResponseBody
    public Object del(String[] ids) {
        Result result = service.delCategory(ids);
        return result;
    }

    @RequestMapping(value = "/categoryTreeNoRoot", name = "-分类树")
    @ResponseBody
    public Object categoryTreeNoRoot(CategoryType categoryType) {
        ZTreeNode treeNode = service.getCategoryTree(categoryType);
        return treeNode.getChildren();
    }

    //转移分类
    @RequestMapping(value = "/trans")
    @ResponseBody
    public Object trans(String sourceId, String targetId) {
        service.trans(sourceId, targetId);
        return new Result(true);
    }

    //类目信息
    @RequestMapping(value = "/info")
    @ResponseBody
    public Object info(String id) {
        Category category = service.get(id);
        return new Result(true, "", category);
    }
}
